#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2020/4/22 15:15
# @Author  : Mr. Cui
# @File    : apis.py
# @Software: PyCharm

from base_server.sqlop import *

class Report(SqlOperate):
    def __init__(self):
        conf = configparser.ConfigParser()
        conf.read("conf/config.ini")
        self.host = conf.get('ci', 'host')
        self.user = conf.get('ci', 'user')
        self.passwd = conf.get('ci', 'passwd')
        self.database = conf.get('ci', 'database')

    def get_date(self,job_name,build_id):
        self.dbcur()
        sql="select job_name,build_id,`type`,case_col,len,successNum,failedNum,url from report where job_name='%s' and build_id='%s'"%(job_name,build_id)
        self.sqlExe(sql)
        self.sqlCom()
        self.sqlclo()
        data = self.cur.fetchall()
        return data

    def get_report_data(self,job_name,build_id):
        data=self.get_date(job_name,build_id)
        yapi=[]
        sonar=[]
        web_ui=[]
        result=0
        for i in data:
            if i[2]==1:
                yapi.append(i)
            elif i[2]==2:
                sonar.append(i)
            else:
                web_ui.append(i)
            result += i[6]
        return result,yapi,sonar,web_ui,result

# (0, [('shop-api-qa.360che.com', 34, 1, '测试集1', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_34_测试集1.html'), ('shop-api-qa.360che.com', 34, 1, '测试集2', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_34_测试集2.html')], [('shop-api-qa.360che.com', 34, 2, '测试集1', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_33_测试集1.html')], [('shop-api-qa.360che.com', 34, 3, '测试集2', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_33_测试集2.html')], 0)

# (('shop-api-qa.360che.com', 34, 2, '测试集1', 4, 3, 1, '/ci/yapi/shop-api-qa.360che.com_33_测试集1.html'), ('shop-api-qa.360che.com', 34, 3, '测试集2', 4, 3, 1, '/ci/yapi/shop-api-qa.360che.com_33_测试集2.html'), ('shop-api-qa.360che.com',34, 1, '测试集1', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_34_测试集1.html'), ('shop-api-qa.360che.com', 34, 1, '测试集2', 4, 3, 0, '/ci/yapi/shop-api-qa.360che.com_34_测试集2.html'))